<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="./css/useredit.css">
    <script src="./js/promiseAjax.js"></script>
    <script src="./js/cookie.js"></script>

</head>

<body>
    <h1>用户密码修改页面</h1>
    <form action="">
        原密码:<input type="text" name="oldpwd">
        <br>
        新密码:<input type="password" name="newpwd">
        <br>
        确认密码:<input type="password" name="repwd">
        <br>
        <input type="submit">
    </form>
</body>
<script>

    document.querySelector('form').onsubmit = function () {
        var oldpwd = document.querySelector('[name="oldpwd"]').value
        var newpwd = document.querySelector('[name="newpwd"]').value
        var repwd = document.querySelector('[name="repwd"]').value
        if (newpwd !== repwd) {
            alert('请输入相同的密码')
        }
        request({
            url: '/api/users/pwdchange/'+ getCookie('userid'),
            method: 'put',
            data: {
                oldpwd,
                newpwd
            },
            headers: {
                Authorization: localStorage.getItem('token')
            }
        }).then((res) => {
            //返回成功消息
            // console.log(res);
            let {msg,error_code}=res
            alert(msg)
            if(error_code===0){
                alert('请重新登录')
                removeCookie('username')
                location.href = './login.html'
            }

        })
        return false
    }

</script>

</html>